package com.pwser.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component
public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
	
	public MyAuthenticationFailureHandler() {
		this.setDefaultFailureUrl("/login?error=true");
	}
	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {
		if (exception instanceof InternalAuthenticationServiceException) {
			request.getSession().setAttribute("msg", exception.getMessage());
		} else {
			request.getSession().setAttribute("msg", "登陆失败：账号或密码不正确，请重新输入");
		}
		super.onAuthenticationFailure(request, response, exception);
	}

}
